/**
 * @ClassName LC38
 * @Author ykx
 * @Date 2024/3/19 10:00
 * @Version 1.0
 */
public class LC38 {
    public static void main(String[] args) {
        LC38 lc38 = new LC38();
        System.out.println(lc38.countAndSay(5));
    }

    public String countAndSay(int n) {
        if (n == 1) {
            return "1";
        } else {
            String s = countAndSay(n - 1);
            StringBuilder result = new StringBuilder("");
            int count = 1;
            for (int i = 0; i < s.length(); i++) {
                if (i > 0 && s.charAt(i) == s.charAt(i - 1)) {
                    count++;
                } else if (i > 0 && s.charAt(i) != s.charAt(i - 1)) {
                    result.append(count).append(s.charAt(i - 1));
                    count = 1;
                }
                if (i == s.length() - 1) {
                    result.append(count).append(s.charAt(i));
                }
            }
            return result.toString();
        }

    }
}
